﻿using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;
using Util;
namespace UtilEditors
{
    public class LogWindow : BaseEditorWindow
    {
        [MenuItem("Tools/辅助/LogWindow")]
        public static void Init()
        {
            Init<LogWindow>();
            debug.log("LogWindow");
            rfe.minSize = new Vector2(300, 200);
        }
        public override void OnEnable()
        {
            base.OnEnable();
            Application.logMessageReceived -= HandleLog;
            Application.logMessageReceived += HandleLog;


            toggleDes = new List<KeyValuePair<int, string>>();
            toggleDes.Add(new KeyValuePair<int, string>(1, "11"));
            toggleDes.Add(new KeyValuePair<int, string>(2, "22"));
            toggleDes.Add(new KeyValuePair<int, string>(3, "33"));
            toggleDes.Add(new KeyValuePair<int, string>(4, "44"));
            toggleDes.Add(new KeyValuePair<int, string>(5, "55"));
            toggleDes.Add(new KeyValuePair<int, string>(6, "66"));

        }
        StringBuilder logBuilder = new StringBuilder();
        Vector2 v2 = new Vector2(0, 0);
        int LWidth = 80;
        int RWidth = 200;
        int HoverID = 0;
        int ClickID;
        IList<KeyValuePair<int, string>> toggleDes;
        void OnGUI()
        {

            StrX();
            {
                int MWidth = position.width.toInt() - LWidth - RWidth;
                ScrollToggles(ref HoverID, ref ClickID, toggleDes, ref v2, LWidth, position.height.toInt());
                ScrollToggles(ref HoverID, ref ClickID, toggleDes, ref v2, MWidth, position.height.toInt());

                StrY();
                {
                    GUILayout.TextArea("TextArea可以多行\n 第二行\n 第二行\n 第二行");
                }
                EndY();
            }
            EndX();
        }
        public class LongData
        {
            public int id;
            public string condition;
            public string stackTrace;
            public LogType type;
            public string key;
            public bool iskeyList;
            public LongData(int _id, string _condition, string _stackTrace, LogType _type)
            {
                this.id = _id;
                this.condition = _condition;
                this.stackTrace = _stackTrace;
                this.type = _type;

                var arr = condition.toSplit('~');
                if (arr.Length > 1)
                {
                    key = arr[0];
                    condition = arr[1];
                }
            }
        }
        public Dictionary<string, List<LongData>> KeyList = new Dictionary<string, List<LongData>>();
        public List<LongData> AllList = new List<LongData>();
        private static int isole = 0;
        public static int iSole { get { return isole++; } }
        public void AddData(LongData data)
        {

            AllList.Add(data);

        }
        void HandleLog(string condition, string stackTrace, LogType type)
        {
            var data = new LongData(iSole, condition, stackTrace, type);
            AddData(data);

            if (type == LogType.Error || type == LogType.Exception)
            {
                string message = string.Format("condition = {0} \n stackTrace = {1} \n type = {2}", condition, stackTrace, type);
                SendLog(message);
            }
        }

        void SendLog(string message)
        {
            logBuilder.Append(message);
        }

        //public override void OnFocus() {
        //    Debug.Log("当窗口获得焦点时调用一次");
        //}
        //
        //public override void OnLostFocus() {
        //    Debug.Log("当窗口丢失焦点时调用一次");
        //}
        //
        //public override void OnHierarchyChange() {
        //    Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");
        //}
        //
        //public override void OnProjectChange() {
        //    Debug.Log("当Project视图中的资源发生改变时调用一次");
        //}
    }
}